package com.micro.business.common.util;

import java.math.BigDecimal;

/**
 * <P>钱和金额的工具类</P>
 *
 * @author 高仕立
 * @date 2019/8/1 9:05 AM
 * @since
 */
public class MoneyUtil {
    
    /**
     * 默认的计算比例100
     */
    public static final BigDecimal oneHundred = new BigDecimal(100);
    
    /**
     * 把元转化为分
     *
     * @param yuanAmount
     * @return java.lang.Integer
     * @author wyq
     * @since 2018/11/2 09:55
     */
    public static Integer yuanToCent(BigDecimal yuanAmount) {
        
        final float zerof = 0.00f;
        final int zeroI = 0;
        if (yuanAmount == null || yuanAmount.intValue() == 0 || yuanAmount.floatValue() == zerof) {
            return zeroI;
        }
        
        //乘100
        final BigDecimal cents = yuanAmount.multiply(oneHundred);
        return cents.intValue();
    }
    
    /**
     * 将分转化为元
     *
     * @param centAmount
     * @return java.lang.Integer
     * @author wyq
     * @since 2018/11/2 15:45
     */
    public static Integer centToYuan(BigDecimal centAmount) {
        
        final float zerof = 0.00f;
        final int zeroI = 0;
        if (centAmount == null || centAmount.intValue() == 0 || centAmount.floatValue() == zerof) {
            return zeroI;
        }
        //不保存小数
        final BigDecimal yuan = centAmount.divide(oneHundred);
        return yuan.intValue();
    }
    
    /**
     * 根据金额获取元(double类型)
     * @param centAmount
     * @return
     */
    public Double centToYuanWithDouble(BigDecimal centAmount) {
        double zero = 0.0d;
        final BigDecimal oneHundred = new BigDecimal(100);
        if (centAmount != null && centAmount.intValue() != 0 && centAmount.doubleValue() != zero) {
            BigDecimal yuan = centAmount.divide(oneHundred);
            return yuan.doubleValue();
        } else {
            return zero;
        }
    }
    
    public static void main(String[] args) {
        
        final Integer result = yuanToCent(new BigDecimal(789));
        final Integer yuan = centToYuan(new BigDecimal(1239000));
        System.out.println(yuan);
        System.out.println(result);
    }
    
}
